package cn.com.self.apis.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.Date;

/**
 * @Description 格式化日期（在指定日期属性前添加@JsonDeserialize(using = CustomJsonDateDeserializer.class)配合使用）
 *
 * @date 2019-03-17
 */
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String date = jsonParser.getText();
        if (StringUtils.isBlank(date)||"null".equals(date)){
            return null;
        }
        Date date1 = DateFormatUtil.parseDate(date);
        if(date1==null){
            AssertUtil.throwException("日期格式转换出错！");
        }
       return date1;
    }
}
